package CelestialWeb;

import CelestialDataAccess.Employee;
import CelestialDataAccess.Customer;
import CelestialDataAccess.User;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.*;
import java.net.URLEncoder;

import CelestialWorkflow.*;
import CelestialDataAccess.*;

public class LoginServlet extends HttpServlet {
   
    /**
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {

        String strEmail = request.getParameter("txtEmail");
        String strPassword = request.getParameter("txtPassword");
        String strRememberMe  = request.getParameter("chkRememberMe");

        /* save email in cookie if customer checked "Remember me" */
        if(strRememberMe.compareToIgnoreCase("true")==0){

            Cookie ckeRememberMe = new Cookie("RememberMe", URLEncoder.encode(strEmail, "UTF-8"));
            ckeRememberMe.setMaxAge(7*24*60*60);
            response.addCookie(ckeRememberMe);
        }
        else{
            Cookie ckeRememberMe = new Cookie("RememberMe", URLEncoder.encode("", "UTF-8"));
            ckeRememberMe.setMaxAge(0);
            response.addCookie(ckeRememberMe);
        }

        LoginControl lgctrl = new LoginControl();
        User usr = lgctrl.validateLogin(strEmail, strPassword);
        if(usr!=null){
            /* customer is authenticated, add cookie to track session */
            Cookie ckeUser = new Cookie("FirstName", URLEncoder.encode(usr.getFirstName(), "UTF-8"));
            response.addCookie(ckeUser);

            Cookie ckeUserLevel = new Cookie("UserLevel", URLEncoder.encode(usr.getEmail(), "UTF-8"));
            response.addCookie(ckeUserLevel);

            if(usr.getUserLevel()==1){
                String strCustomerID = Integer.toString(((Customer)usr).getCustomerID());
                Cookie ckeCustomerID = new Cookie("CustomerID", URLEncoder.encode(strCustomerID, "UTF-8"));
                response.addCookie(ckeCustomerID);
            }
            else{
                assert(usr.getUserLevel() > 1);

                String strEmployeeID = Integer.toString(((Employee)usr).getEmployeeID());
                Cookie ckeEmployeeID = new Cookie("EmployeeID", URLEncoder.encode(strEmployeeID, "UTF-8"));
                response.addCookie(ckeEmployeeID);
            }
        }
        else{
            /* authentication fails, see what's the error */
            response.setContentType("text/xml");
            response.setHeader("Cache-control", "no-cache");
            PrintWriter pw = response.getWriter();

            assert lgctrl.getError() > 0;
            pw.println("<ErrorCode>" + lgctrl.getError() + "</ErrorCode>");
            pw.close();
        }
    } 
    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    } 

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
